Last updated: 2024-09-13

Checks: 6 1

Knit directory: mutation_rate/

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


The R Markdown file has unstaged changes. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish to commit the R Markdown file and build the HTML.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20230228) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 6d64883. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory

Untracked files:
    Untracked:  analysis/denovo0422_realdata_CV_mssng_halftt_bin100bp.Rmd

Unstaged changes:
    Modified:   analysis/denovo0422_realdata_CV_mssng_halftt.Rmd
    Modified:   analysis/denovo0422_realdata_summary.Rmd

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/denovo0422_realdata_CV_mssng_halftt.Rmd) and HTML (docs/denovo0422_realdata_CV_mssng_halftt.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 6d64883 XSun 2024-09-04 update
html 6d64883 XSun 2024-09-04 update
Rmd bb49323 XSun 2024-09-02 update
html bb49323 XSun 2024-09-02 update
Rmd b10673e XSun 2024-08-30 update
html b10673e XSun 2024-08-30 update
Rmd d7785e1 XSun 2024-08-30 update
html d7785e1 XSun 2024-08-30 update
Rmd 1cfb705 XSun 2024-08-23 update
html 1cfb705 XSun 2024-08-23 update

Introduction

We cleaned the real data here.

library(ggplot2)
library(gridExtra)
library(data.table)

scatter_plot_mutct <- function(data, xcol, ycol, title=NULL) {
  # Fit the linear model using user-defined columns
  formula <- as.formula(paste(ycol, "~", xcol, "+0"))
  fit <- lm(formula, data = data)
  adj_rsq <- summary(fit)$adj.r.squared  # Extract the adjusted R-squared

  x_values <- data[[xcol]]
  y_values <- data[[ycol]]

  # Calculate the LPD (Log Predictive Density)
  lpd <- sum(y_values * log(x_values),na.rm = T) - sum(x_values)
  
  ggplot(data) +
  geom_point(data = data, aes_string(x = xcol, y = ycol), color = "black") +
  geom_abline(intercept = 0, slope = 1, color = "red", linetype = "dashed") +
  annotate("text", x = Inf, y = Inf, label = paste0("Adj R-sq = ", round(adj_rsq, 3), 
                                                    "\nLPD = ", round(lpd, 3)),
           hjust = 1.1, vjust = 1.1, color = "blue", parse = FALSE) +  # Adjust text positioning and color
  labs(x = paste("Predicted mutation count:", xcol),
       y = paste("Observed mutation count:", ycol),
       title = title) +
  theme_minimal()
  
}

# scatter_plot_mutct <- function(data, xcol, ycol, title=NULL) {
#   # Fit the linear model using user-defined columns
#   formula <- as.formula(paste(ycol, "~", xcol, "+0"))
#   fit <- lm(formula, data = data)
#   adj_rsq <- summary(fit)$adj.r.squared  # Extract the adjusted R-squared
#   
#   ggplot(data) +
#   geom_point(data = data, aes_string(x = xcol, y = ycol),color = "black") +
#   geom_abline(intercept = 0, slope = 1, color = "red", linetype = "dashed") +
#   annotate("text", x = Inf, y = Inf, label = paste0("Adj R-sq = ", round(adj_rsq, 3)),
#            hjust = 1.1, vjust = 1.1, color = "blue", parse = FALSE) +  # Adjust text positioning and color
#   labs(x = paste("Predicted mutation count:", xcol),
#        y = paste("Observed mutation count:", ycol),
#        title = title) +
#   theme_minimal()
#   
# }

plot_scatter_randeff <- function(data) {
  ggplot(data = df_compare) +aes(x=seg_est,y=chr_est) +
  geom_point() +
  labs(x = "random effects estimated from partitioned segments",
       y= "random effects estimated from whole chromosome") +
  geom_abline(slope = 1, intercept = 0, col="red") +
  theme_minimal()
}

plot_randeff_genome <- function(data) {
  ggplot(df_compare, aes(x =  window_start/1000000)) +
  geom_line(aes(y = seg_est, color = "estimated from partitioned segments"), alpha = 0.4) +
  geom_line(aes(y = chr_est, color = "estimated from whole chr"), alpha = 0.3) +
  geom_line(aes(y = fold_change, color = "observed fold change"), alpha = 0.1) +
  labs(x = "gemonic position (mb)",
       y = "estimated random effects") +
  scale_color_manual(name = "Group",
                     values = c("estimated from partitioned segments" = "blue",
                                "estimated from whole chr" = "green", "observed fold change" = "red"),
                     labels = c("estimated from partitioned segments","estimated from whole chr", "observed fold change")) +
  theme_minimal()
}


plot_randeff_windows <- function(data) {
  
  ggplot(data, aes(x = Window_Start/1000000)) +
  geom_line(aes(y = randeff_est, color = "Estimated random effect"), alpha = 0.4) +
  geom_line(aes(y = rl_rescaled_sum, color = "Roulette predicted mutation count"), alpha = 0.3) +
  geom_line(aes(y = obs_sum, color = "Observed de novo mutations"), alpha = 0.1) +
  labs(x = "Genomic position (Mb)",
       y = "Estimated random effects") +
  scale_color_manual(name = "Group",
                     values = c("Estimated random effect" = "blue",
                                "Roulette predicted mutation count" = "green", 
                                "Observed de novo mutations" = "red")) +
  guides(color = guide_legend(override.aes = list(alpha = 1))) +
  theme_minimal()
  
}

Setting one: partition the gaps based on large gaps

  • For large gap: treat as different segments. So for chr1, there will be 3 segments.
  • For small gaps like 50kb, treat them as continuous ones.

The whole window prediction

  • Run smashgen on the whole window, to get an estimation for the local random effects

fit = ebps(df_seg_non0$obs_sum,df_seg_non0$rl_rescaled_sum,smooth_control = list(wave_trans='ndwt',ndwt_method='smash'), general_control = list(verbose=T,printevery=1, maxiter=50))

  • Predict the mutation rate on the whole window, based on the rescaled Roulette estimation.

df_per_window_data$sum_pred <- df_per_window_data$randeff_est*df_per_window_data$rl_rescaled_sum

10kb

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_10kb_fitted_testpred_halftt.rdata")

df_per_window_data$sum_pred <- df_per_window_data$randeff_est*df_per_window_data$rl_rescaled_sum
data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
Warning in instance$preRenderHook(instance): It seems your data is too
big for client-side DataTables. You may consider server-side processing:
https://rstudio.github.io/DT/server.html
p1 <- scatter_plot_mutct(data = data, xcol = "sum_pred", ycol = "obs_sum",title = "smashgen prediction")
Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
i Please use tidy evaluation idioms with `aes()`.
i See also `vignette("ggplot2-in-packages")` for more information.
This warning is displayed once every 8 hours.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
generated.
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_sum", ycol = "obs_sum",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Version Author Date
6d64883 XSun 2024-09-04

30kb

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_30kb_fitted_testpred_halftt.rdata")

df_per_window_data$sum_pred <- df_per_window_data$randeff_est*df_per_window_data$rl_rescaled_sum
data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "sum_pred", ycol = "obs_sum",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_sum", ycol = "obs_sum",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Version Author Date
6d64883 XSun 2024-09-04

50kb

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_50kb_fitted_testpred_halftt.rdata")

df_per_window_data$sum_pred <- df_per_window_data$randeff_est*df_per_window_data$rl_rescaled_sum
data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "sum_pred", ycol = "obs_sum",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_sum", ycol = "obs_sum",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Version Author Date
6d64883 XSun 2024-09-04

100kb

SMASHGEN mean

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_100kb_fitted_testpred_halftt.rdata")

df_per_window_data$sum_pred <- df_per_window_data$randeff_est*df_per_window_data$rl_rescaled_sum
data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "sum_pred", ycol = "obs_sum",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_sum", ycol = "obs_sum",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Version Author Date
6d64883 XSun 2024-09-04
print("fitting output example")
[1] "fitting output example"
load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/fit_example_100kb_seg1.rdata")
str(fit)
List of 5
 $ posterior     :List of 4
  ..$ mean           : num [1:1198] 0.239 0.238 0.253 1.41 1.976 ...
  ..$ mean_log       : num [1:1198] -1.699 -1.684 -1.605 0.291 0.64 ...
  ..$ mean_smooth    : num [1:1198] 0.356 0.41 0.514 0.716 0.95 ...
  ..$ mean_log_smooth: num [1:1198] -1.034 -0.892 -0.665 -0.335 -0.051 ...
 $ log_likelihood: NULL
 $ elbo_trace    : num [1:8] -Inf -11561 -11518 -11504 -11499 ...
 $ fitted_g      :List of 2
  ..$ sigma2      : num 0.868
  ..$ sigma2_trace: num [1:8] 1.369 1.235 1.118 1.031 0.968 ...
 $ run_time      : 'difftime' num 61.0477244853973
  ..- attr(*, "units")= chr "secs"
ggplot(data, aes(x = randeff_est)) + 
  geom_histogram(binwidth = 0.5, fill = "skyblue", color = "black") +
  labs(title = "Histogram of estimated random effects ",
       x = "Estimated random effects ") +
  geom_vline(xintercept = 1, color = "red", linetype = "dashed", size = 1.2) +
  annotate("text", x = 1, y = Inf, label = "x = 1", vjust = 2, color = "red")
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
i Please use `linewidth` instead.
This warning is displayed once every 8 hours.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
generated.

Version Author Date
b10673e XSun 2024-08-30
1cfb705 XSun 2024-08-23

We plot some regions to check the results.

print("Windows with large estimated random effects")
[1] "Windows with large estimated random effects"
plot_data <- data[data$Window %in% c(400:500),]
plot_randeff_windows(plot_data)

Version Author Date
b10673e XSun 2024-08-30
1cfb705 XSun 2024-08-23
print("Windows with small estimated random effects")
[1] "Windows with small estimated random effects"
plot_data <- data[data$Window %in% c(750:850),]
plot_randeff_windows(plot_data)

plot_data <- data[data$Window %in% c(1850:2100),]
plot_randeff_windows(plot_data)

SMASHGEN mean_smooth

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_100kb_fitted_testpred_halftt_smooth.rdata")

df_per_window_data$sum_pred <- df_per_window_data$randeff_est*df_per_window_data$rl_rescaled_sum
data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "sum_pred", ycol = "obs_sum",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_sum", ycol = "obs_sum",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

ggplot(data, aes(x = randeff_est)) + 
  geom_histogram(binwidth = 0.5, fill = "skyblue", color = "black") +
  labs(title = "Histogram of estimated random effects ",
       x = "Estimated random effects ") +
  geom_vline(xintercept = 1, color = "red", linetype = "dashed", size = 1.2) +
  annotate("text", x = 1, y = Inf, label = "x = 1", vjust = 2, color = "red")

Version Author Date
1cfb705 XSun 2024-08-23

We plot some regions to check the results.

print("Windows with large estimated random effects")
[1] "Windows with large estimated random effects"
plot_data <- data[data$Window %in% c(400:500),]
plot_randeff_windows(plot_data)

Version Author Date
1cfb705 XSun 2024-08-23
print("Windows with small estimated random effects")
[1] "Windows with small estimated random effects"
plot_data <- data[data$Window %in% c(750:850),]
plot_randeff_windows(plot_data)

Version Author Date
6d64883 XSun 2024-09-04
plot_data <- data[data$Window %in% c(1850:2100),]
plot_randeff_windows(plot_data)

Non-spatial only (computed from gamma estimation)

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_100kb_fitted_testpred_halftt_gammaonly.rdata")

sprintf("estimated alpha = %s", unique(df_per_window_data$alpha_est))
[1] "estimated alpha = 0.770703125"
df_per_window_data$sum_pred <- df_per_window_data$randeff_est*df_per_window_data$rl_rescaled_sum
data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "sum_pred", ycol = "obs_sum",title = "Gamma prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_sum", ycol = "obs_sum",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Version Author Date
6d64883 XSun 2024-09-04
ggplot(data, aes(x = randeff_est)) + 
  geom_histogram(binwidth = 0.5, fill = "skyblue", color = "black") +
  labs(title = "Histogram of estimated random effects",
       x = "Estimated random effects") +
  geom_vline(xintercept = 1, color = "red", linetype = "dashed", size = 1.2) +
  annotate("text", x = 1, y = Inf, label = "x = 1", vjust = 2, color = "red")

Version Author Date
d7785e1 XSun 2024-08-30
1cfb705 XSun 2024-08-23

We plot some regions to check the results.

print("Windows with large estimated random effects")
[1] "Windows with large estimated random effects"
plot_data <- data[data$Window %in% c(400:500),]
plot_randeff_windows(plot_data)

Version Author Date
d7785e1 XSun 2024-08-30
1cfb705 XSun 2024-08-23
print("Windows with small estimated random effects")
[1] "Windows with small estimated random effects"
plot_data <- data[data$Window %in% c(750:850),]
plot_randeff_windows(plot_data)

Version Author Date
6d64883 XSun 2024-09-04
plot_data <- data[data$Window %in% c(1850:2100),]
plot_randeff_windows(plot_data)

Cross validation

We partition each window into 1kb bins, use odd bins to train the local effect, and test on the even bins

  • Run smashgen on the training data, to get an estimation for the local random effects

fit = ebps(df_seg_non0$obs_train,df_seg_non0$rl_rescaled_train,smooth_control = list(wave_trans='ndwt',ndwt_method='smash'), general_control = list(verbose=T,printevery=1, maxiter=50))

  • Predict the mutation rate on testing data, based on the rescaled Roulette estimation.

df_per_window_data$test_pred <- df_per_window_data$randeff_est_train*df_per_window_data$rl_rescaled_test

10kb

smashgen mean

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_10kb_fitted_testpred_halftt.rdata")

data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
Warning in instance$preRenderHook(instance): It seems your data is too
big for client-side DataTables. You may consider server-side processing:
https://rstudio.github.io/DT/server.html
p1 <- scatter_plot_mutct(data = data, xcol = "test_pred", ycol = "obs_test",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_test", ycol = "obs_test",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Version Author Date
6d64883 XSun 2024-09-04

smashgen mean_smooth

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_10kb_fitted_testpred_halftt_smooth.rdata")

data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
Warning in instance$preRenderHook(instance): It seems your data is too
big for client-side DataTables. You may consider server-side processing:
https://rstudio.github.io/DT/server.html
p1 <- scatter_plot_mutct(data = data, xcol = "test_pred", ycol = "obs_test",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_test", ycol = "obs_test",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Version Author Date
6d64883 XSun 2024-09-04

gamma non-spatial

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_10kb_fitted_testpred_halftt_gammaonly.rdata")

sprintf("estimated alpha = %s", unique(df_per_window_data$alpha_est))
[1] "estimated alpha = 0.501171875"
data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
Warning in instance$preRenderHook(instance): It seems your data is too
big for client-side DataTables. You may consider server-side processing:
https://rstudio.github.io/DT/server.html
p1 <- scatter_plot_mutct(data = data, xcol = "test_pred", ycol = "obs_test",title = "Gamma prediction prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_test", ycol = "obs_test",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Version Author Date
6d64883 XSun 2024-09-04

30kb

smashgen mean

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_30kb_fitted_testpred_halftt.rdata")

data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "test_pred", ycol = "obs_test",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_test", ycol = "obs_test",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Version Author Date
6d64883 XSun 2024-09-04
1cfb705 XSun 2024-08-23

smashgen mean_smooth

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_30kb_fitted_testpred_halftt_smooth.rdata")

data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "test_pred", ycol = "obs_test",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_test", ycol = "obs_test",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Version Author Date
1cfb705 XSun 2024-08-23

gamma non-spatial

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_30kb_fitted_testpred_halftt_gammaonly.rdata")

sprintf("estimated alpha = %s", unique(df_per_window_data$alpha_est))
[1] "estimated alpha = 0.649609375"
data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "test_pred", ycol = "obs_test",title = "Gamma prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_test", ycol = "obs_test",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Version Author Date
d7785e1 XSun 2024-08-30
1cfb705 XSun 2024-08-23

50kb

smashgen mean

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_50kb_fitted_testpred_halftt.rdata")

data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "test_pred", ycol = "obs_test",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_test", ycol = "obs_test",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Version Author Date
d7785e1 XSun 2024-08-30
1cfb705 XSun 2024-08-23

smashgen mean_smooth

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_50kb_fitted_testpred_halftt_smooth.rdata")

data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "test_pred", ycol = "obs_test",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_test", ycol = "obs_test",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Version Author Date
d7785e1 XSun 2024-08-30

gamma non-spatial

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_50kb_fitted_testpred_halftt_gammaonly.rdata")

sprintf("estimated alpha = %s", unique(df_per_window_data$alpha_est))
[1] "estimated alpha = 0.691015625"
data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "test_pred", ycol = "obs_test",title = "Gamma prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_test", ycol = "obs_test",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Version Author Date
d7785e1 XSun 2024-08-30

100kb

smashgen mean

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_100kb_fitted_testpred_halftt.rdata")

data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "test_pred", ycol = "obs_test",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_test", ycol = "obs_test",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Version Author Date
d7785e1 XSun 2024-08-30

There are some windows with large observed mutation rate on testing set, but their expected rates are very low.

data_large <- data[order(data$obs_test,decreasing = T),][1:5,]
data_large <- data_large[order(data_large$obs_test, decreasing = T),]

colnames(data_large)[c(7,10,13,16)] <- c("rl_rescaled_wholewindow","obs_wholewindow","foldchange_wholewindow","randeff_est_wholewindow")
DT::datatable(data_large,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','windows with large observed mutation rate on testing set, but low expected rates'),options = list(pageLength = 10) )

We look into the window 2254. We varied the bin sizes (500bp, 1kb, 2kb) and checked how this window is divided.

obs <- fread("/project/xinhe/xsun/mutation_rate/data/trost_denovo/processed/alltype.mssng.bed")
colnames(obs) <- c("CHROM","START","REF","ALT")
obs_window <- obs[obs$START > 225310007 & obs$START < 225410006 & obs$CHROM ==1, ]

start_window <- 225310007
end_window <- 225410006
bin_sizes <- c(100, 500, 1000, 2000)

# Function to assign training or testing based on bin size
assign_bin_varying_size <- function(start_position, bin_size) {
  bins <- seq(start_window, end_window, by = bin_size)
  bin_index <- findInterval(start_position, bins)
  if (bin_index %% 2 == 1) {
    return("training")
  } else {
    return("testing")
  }
}

# Loop over the different bin sizes and add a new column for each
for (bin_size in bin_sizes) {
  column_name <- paste0("set_", bin_size)
  obs_window[[column_name]] <- sapply(obs_window$START, assign_bin_varying_size, bin_size = bin_size)
}

DT::datatable(obs_window,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','how the window is divided when bin size varies'),options = list(pageLength = 10) )
sprintf("bin size 100bp, number of training= %s, number of testing = %s",sum(obs_window$set_100 =="training"),sum(obs_window$set_100 =="testing"))
[1] "bin size 100bp, number of training= 22, number of testing = 10"
sprintf("bin size 500bp, number of training= %s, number of testing = %s",sum(obs_window$set_500 =="training"),sum(obs_window$set_500 =="testing"))
[1] "bin size 500bp, number of training= 21, number of testing = 11"
sprintf("bin size 1000bp, number of training= %s, number of testing = %s",sum(obs_window$set_1000 =="training"),sum(obs_window$set_1000 =="testing"))
[1] "bin size 1000bp, number of training= 4, number of testing = 28"
sprintf("bin size 2000bp, number of training= %s, number of testing = %s",sum(obs_window$set_2000 =="training"),sum(obs_window$set_2000 =="testing"))
[1] "bin size 2000bp, number of training= 27, number of testing = 5"

We also look into the window 315.

obs_window <- obs[obs$START > 31410007 & obs$START < 31510006 & obs$CHROM ==1, ]

start_window <- 31410007
end_window <- 31510006
bin_sizes <- c(100, 500, 1000, 2000)

# Function to assign training or testing based on bin size
assign_bin_varying_size <- function(start_position, bin_size) {
  bins <- seq(start_window, end_window, by = bin_size)
  bin_index <- findInterval(start_position, bins)
  if (bin_index %% 2 == 1) {
    return("training")
  } else {
    return("testing")
  }
}

# Loop over the different bin sizes and add a new column for each
for (bin_size in bin_sizes) {
  column_name <- paste0("set_", bin_size)
  obs_window[[column_name]] <- sapply(obs_window$START, assign_bin_varying_size, bin_size = bin_size)
}

DT::datatable(obs_window,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','how the window is divided when bin size varies'),options = list(pageLength = 10) )
sprintf("bin size 100bp, number of training= %s, number of testing = %s",sum(obs_window$set_100 =="training"),sum(obs_window$set_100 =="testing"))
[1] "bin size 100bp, number of training= 29, number of testing = 3"
sprintf("bin size 500bp, number of training= %s, number of testing = %s",sum(obs_window$set_500 =="training"),sum(obs_window$set_500 =="testing"))
[1] "bin size 500bp, number of training= 7, number of testing = 25"
sprintf("bin size 1000bp, number of training= %s, number of testing = %s",sum(obs_window$set_1000 =="training"),sum(obs_window$set_1000 =="testing"))
[1] "bin size 1000bp, number of training= 8, number of testing = 24"
sprintf("bin size 2000bp, number of training= %s, number of testing = %s",sum(obs_window$set_2000 =="training"),sum(obs_window$set_2000 =="testing"))
[1] "bin size 2000bp, number of training= 25, number of testing = 7"

There are also some windows having high predicted rates but low observed mutations on testing set.

data_small <- data[order(data$test_pred,decreasing = T),][1:10,]

colnames(data_small)[c(7,10,13,16)] <- c("rl_rescaled_wholewindow","obs_wholewindow","foldchange_wholewindow","randeff_est_wholewindow")
DT::datatable(data_small,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','windows with large expected rates'),options = list(pageLength = 10) )

We look into the window 454.

obs_window <- obs[obs$START > 45310007 & obs$START < 45410006 & obs$CHROM ==1, ]

start_window <- 45310007
end_window <- 45410006
bin_sizes <- c(500, 1000, 2000)

# Function to assign training or testing based on bin size
assign_bin_varying_size <- function(start_position, bin_size) {
  bins <- seq(start_window, end_window, by = bin_size)
  bin_index <- findInterval(start_position, bins)
  if (bin_index %% 2 == 1) {
    return("training")
  } else {
    return("testing")
  }
}

# Loop over the different bin sizes and add a new column for each
for (bin_size in bin_sizes) {
  column_name <- paste0("set_", bin_size)
  obs_window[[column_name]] <- sapply(obs_window$START, assign_bin_varying_size, bin_size = bin_size)
}

DT::datatable(obs_window,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','how the window is divided when bin size varies'),options = list(pageLength = 10) )
sprintf("bin size 500bp, number of training= %s, number of testing = %s",sum(obs_window$set_500 =="training"),sum(obs_window$set_500 =="testing"))
[1] "bin size 500bp, number of training= 43, number of testing = 7"
sprintf("bin size 1000bp, number of training= %s, number of testing = %s",sum(obs_window$set_1000 =="training"),sum(obs_window$set_1000 =="testing"))
[1] "bin size 1000bp, number of training= 48, number of testing = 2"
sprintf("bin size 2000bp, number of training= %s, number of testing = %s",sum(obs_window$set_2000 =="training"),sum(obs_window$set_2000 =="testing"))
[1] "bin size 2000bp, number of training= 44, number of testing = 6"

smashgen mean_smooth

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_100kb_fitted_testpred_halftt_smooth.rdata")

data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "test_pred", ycol = "obs_test",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_test", ycol = "obs_test",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

gamma non-spatial

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_100kb_fitted_testpred_halftt_gammaonly.rdata")

sprintf("estimated alpha = %s", unique(df_per_window_data$alpha_est))
[1] "estimated alpha = 0.770703125"
data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "test_pred", ycol = "obs_test",title = "Gamma prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_test", ycol = "obs_test",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Setting two: treat the whole genome as one segment

The whole window prediction

10kb

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_10kb_fitted_testpred_chr_halftt.rdata")

df_per_window_data$sum_pred <- df_per_window_data$randeff_est*df_per_window_data$rl_rescaled_sum
data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
Warning in instance$preRenderHook(instance): It seems your data is too
big for client-side DataTables. You may consider server-side processing:
https://rstudio.github.io/DT/server.html
p1 <- scatter_plot_mutct(data = data, xcol = "sum_pred", ycol = "obs_sum",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_sum", ycol = "obs_sum",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

30kb

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_30kb_fitted_testpred_chr_halftt.rdata")

df_per_window_data$sum_pred <- df_per_window_data$randeff_est*df_per_window_data$rl_rescaled_sum
data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "sum_pred", ycol = "obs_sum",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_sum", ycol = "obs_sum",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

50kb

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_50kb_fitted_testpred_chr_halftt.rdata")

df_per_window_data$sum_pred <- df_per_window_data$randeff_est*df_per_window_data$rl_rescaled_sum
data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "sum_pred", ycol = "obs_sum",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_sum", ycol = "obs_sum",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

100kb

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_100kb_fitted_testpred_chr_halftt.rdata")

df_per_window_data$sum_pred <- df_per_window_data$randeff_est*df_per_window_data$rl_rescaled_sum
data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "sum_pred", ycol = "obs_sum",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_sum", ycol = "obs_sum",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Cross validation

We partition each window into 1kb bins, use odd bins to train the local effect, and test on the even bins

  • Run smashgen on the training data, to get an estimation for the local random effects

fit = ebps(df_seg_non0$obs_train,df_seg_non0$rl_rescaled_train,smooth_control = list(wave_trans='ndwt',ndwt_method='smash'), general_control = list(verbose=T,printevery=1, maxiter=50))

  • Predict the mutation rate on testing data, based on the rescaled Roulette estimation.

df_per_window_data$test_pred <- df_per_window_data$randeff_est_train*df_per_window_data$rl_rescaled_test

10kb

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_10kb_fitted_testpred_chr_halftt.rdata")

data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
Warning in instance$preRenderHook(instance): It seems your data is too
big for client-side DataTables. You may consider server-side processing:
https://rstudio.github.io/DT/server.html
p1 <- scatter_plot_mutct(data = data, xcol = "test_pred", ycol = "obs_test",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_test", ycol = "obs_test",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

30kb

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_30kb_fitted_testpred_chr_halftt.rdata")

data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "test_pred", ycol = "obs_test",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_test", ycol = "obs_test",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

50kb

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_50kb_fitted_testpred_chr_halftt.rdata")

data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "test_pred", ycol = "obs_test",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_test", ycol = "obs_test",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

100kb

load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_100kb_fitted_testpred_chr_halftt.rdata")

data <- df_per_window_data[complete.cases(df_per_window_data$test_pred),]

DT::datatable(data,caption = htmltools::tags$caption( style = 'caption-side: left; text-align: left; color:black;  font-size:150% ;','Details for each window'),options = list(pageLength = 5) )
p1 <- scatter_plot_mutct(data = data, xcol = "test_pred", ycol = "obs_test",title = "smashgen prediction")
p2 <- scatter_plot_mutct(data = data, xcol = "rl_rescaled_test", ycol = "obs_test",title = "Roulette baseline")

grid.arrange(p1, p2, ncol = 2)

Comparing the random effects estimated by smashgen for setting 1 and 2

10kb

df_seg <- get(load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_10kb_fitted_testpred_halftt.rdata"))
df_chr <- get(load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_10kb_fitted_testpred_chr_halftt.rdata"))


df_compare <- data.frame(seg_est = df_seg$randeff_est,
                         chr_est = df_chr$randeff_est,
                         fold_change = df_seg$obs_sum/df_seg$randeff_est,
                         window_start = df_seg$Window_Start)


ggplot(data = df_compare) +aes(x=seg_est,y=chr_est) +
  geom_point() +
  labs(x = "random effects estimated from partitioned segments",
       y= "random effects estimated from whole chromosome") +
  geom_abline(slope = 1, intercept = 0, col="red") +
  theme_minimal()

ggplot(df_compare, aes(x =  window_start/1000000)) +
  geom_line(aes(y = seg_est, color = "estimated from partitioned segments"), alpha = 0.4) +
  geom_line(aes(y = chr_est, color = "estimated from whole chr"), alpha = 0.3) +
  geom_line(aes(y = fold_change, color = "observed fold change"), alpha = 0.1) +
  labs(x = "gemonic position (mb)",
       y = "estimated random effects") +
  scale_color_manual(name = "Group",
                     values = c("estimated from partitioned segments" = "blue",
                                "estimated from whole chr" = "green", "observed fold change" = "red"),
                     labels = c("estimated from partitioned segments","estimated from whole chr", "observed fold change")) +
  theme_minimal()

30kb

df_seg <- get(load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_30kb_fitted_testpred_halftt.rdata"))
df_chr <- get(load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_30kb_fitted_testpred_chr_halftt.rdata"))


df_compare <- data.frame(seg_est = df_seg$randeff_est,
                         chr_est = df_chr$randeff_est,
                         fold_change = df_seg$obs_sum/df_seg$randeff_est,
                         window_start = df_seg$Window_Start)


ggplot(data = df_compare) +aes(x=seg_est,y=chr_est) +
  geom_point() +
  labs(x = "random effects estimated from partitioned segments",
       y= "random effects estimated from whole chromosome") +
  geom_abline(slope = 1, intercept = 0, col="red") +
  theme_minimal()

ggplot(df_compare, aes(x =  window_start/1000000)) +
  geom_line(aes(y = seg_est, color = "estimated from partitioned segments"), alpha = 0.4) +
  geom_line(aes(y = chr_est, color = "estimated from whole chr"), alpha = 0.3) +
  geom_line(aes(y = fold_change, color = "observed fold change"), alpha = 0.1) +
  labs(x = "gemonic position (mb)",
       y = "estimated random effects") +
  scale_color_manual(name = "Group",
                     values = c("estimated from partitioned segments" = "blue",
                                "estimated from whole chr" = "green", "observed fold change" = "red"),
                     labels = c("estimated from partitioned segments","estimated from whole chr", "observed fold change")) +
  theme_minimal()

50kb

df_seg <- get(load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_50kb_fitted_testpred_halftt.rdata"))
df_chr <- get(load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_50kb_fitted_testpred_chr_halftt.rdata"))


df_compare <- data.frame(seg_est = df_seg$randeff_est,
                         chr_est = df_chr$randeff_est,
                         fold_change = df_seg$obs_sum/df_seg$randeff_est,
                         window_start = df_seg$Window_Start)


ggplot(data = df_compare) +aes(x=seg_est,y=chr_est) +
  geom_point() +
  labs(x = "random effects estimated from partitioned segments",
       y= "random effects estimated from whole chromosome") +
  geom_abline(slope = 1, intercept = 0, col="red") +
  theme_minimal()

ggplot(df_compare, aes(x =  window_start/1000000)) +
  geom_line(aes(y = seg_est, color = "estimated from partitioned segments"), alpha = 0.4) +
  geom_line(aes(y = chr_est, color = "estimated from whole chr"), alpha = 0.3) +
  geom_line(aes(y = fold_change, color = "observed fold change"), alpha = 0.1) +
  labs(x = "gemonic position (mb)",
       y = "estimated random effects") +
  scale_color_manual(name = "Group",
                     values = c("estimated from partitioned segments" = "blue",
                                "estimated from whole chr" = "green", "observed fold change" = "red"),
                     labels = c("estimated from partitioned segments","estimated from whole chr", "observed fold change")) +
  theme_minimal()

100kb

df_seg <- get(load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_100kb_fitted_testpred_halftt.rdata"))
df_chr <- get(load("/project/xinhe/xsun/mutation_rate/11.denovo_real_mssng/results/mssng_trost_per_window_chr1_100kb_fitted_testpred_chr_halftt.rdata"))


df_compare <- data.frame(seg_est = df_seg$randeff_est,
                         chr_est = df_chr$randeff_est,
                         fold_change = df_seg$obs_sum/df_seg$randeff_est,
                         window_start = df_seg$Window_Start)


ggplot(data = df_compare) +aes(x=seg_est,y=chr_est) +
  geom_point() +
  labs(x = "random effects estimated from partitioned segments",
       y= "random effects estimated from whole chromosome") +
  geom_abline(slope = 1, intercept = 0, col="red") +
  theme_minimal()

ggplot(df_compare, aes(x =  window_start/1000000)) +
  geom_line(aes(y = seg_est, color = "estimated from partitioned segments"), alpha = 0.4) +
  geom_line(aes(y = chr_est, color = "estimated from whole chr"), alpha = 0.3) +
  geom_line(aes(y = fold_change, color = "observed fold change"), alpha = 0.1) +
  labs(x = "gemonic position (mb)",
       y = "estimated random effects") +
  scale_color_manual(name = "Group",
                     values = c("estimated from partitioned segments" = "blue",
                                "estimated from whole chr" = "green", "observed fold change" = "red"),
                     labels = c("estimated from partitioned segments","estimated from whole chr", "observed fold change")) +
  theme_minimal()


sessionInfo()
R version 4.2.0 (2022-04-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS/LAPACK: /software/openblas-0.3.13-el7-x86_64/lib/libopenblas_haswellp-r0.3.13.so

locale:
[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.14.2 gridExtra_2.3     ggplot2_3.5.1    

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.12       highr_0.9         pillar_1.9.0      compiler_4.2.0   
 [5] bslib_0.3.1       later_1.3.0       jquerylib_0.1.4   git2r_0.30.1     
 [9] workflowr_1.7.0   tools_4.2.0       digest_0.6.29     jsonlite_1.8.0   
[13] evaluate_0.15     lifecycle_1.0.4   tibble_3.2.1      gtable_0.3.0     
[17] pkgconfig_2.0.3   rlang_1.1.2       cli_3.6.1         rstudioapi_0.13  
[21] crosstalk_1.2.0   yaml_2.3.5        xfun_0.41         fastmap_1.1.0    
[25] withr_2.5.0       dplyr_1.1.4       stringr_1.5.1     knitr_1.39       
[29] htmlwidgets_1.5.4 generics_0.1.2    fs_1.5.2          vctrs_0.6.5      
[33] sass_0.4.1        DT_0.22           tidyselect_1.2.0  rprojroot_2.0.3  
[37] grid_4.2.0        glue_1.6.2        R6_2.5.1          fansi_1.0.3      
[41] rmarkdown_2.25    farver_2.1.0      magrittr_2.0.3    whisker_0.4      
[45] scales_1.3.0      promises_1.2.0.1  htmltools_0.5.2   colorspace_2.0-3 
[49] httpuv_1.6.5      labeling_0.4.2    utf8_1.2.2        stringi_1.7.6    
[53] munsell_0.5.0     crayon_1.5.1